<%@page import="org.upt.dcda.db.BookReviewEntity"%>
<%@page import="org.upt.dcda.db.BookEntity"%>
<%@page import="org.upt.dcda.db.UserEntity"%>
<%@page import="org.upt.dcda.presentation.SessionAttributes"%>
<%@ taglib prefix="stripes"
	uri="http://stripes.sourceforge.net/stripes.tld"%>
<stripes:layout-render name="layout/default.jsp"
	pageTitle="Using A Layout">

	<stripes:layout-component name="pageTitle">
	Book
 	</stripes:layout-component>

	<stripes:layout-component name="html_head">
		<script type="text/javascript">
	
	function getBookInfo(id)
	{
		$.getJSON("services/books/getInfo?id=" + id, function(data) {
				$('#bookInfo > tbody:last').append(
						'<tr>' + 
						'<td>' + data.name + '</td>' + 
						'<td>' + '<a href="displayauthor.jsp?id=' + data.authorId + '">' + data.author + '</a>' + '</td>' + 
						'<td>' + data.price + '</td>' + 
						'<td>' + data.description + '</td>');
				<%if (SessionAttributes.isAdmin(request)) {%>
				$('#bookInfo > tbody:last').append('<td>' + data.quantity + '</td>');
				<%}%>
				if(data.quantity > 0) {
					$('#bookInfo > tbody:last').append(
						'<td><button onclick="addToBasket(' + data.id + ',' + '1'  + ')">Add to basket</button></td>' +
						'</tr>');
				} else {
					<%if (SessionAttributes.isLoggedIn(request)) {%>
					$('#bookInfo > tbody:last').append(
						'<td><form action="addnotification" method="post"><input type="hidden" name="bookId" value="' +
						data.id + '"> <input type=submit value="Notify me when available"></form></td>' +
						'</tr>');
					<%} else {%>
					$('#bookInfo > tbody:last').append(
												'</tr>');
										<%}%>
									}
								});
			}
	
			function getBookReview(id) {
				$.getJSON("services/books/getBookReview?id=" + id, function(
						data) {
					$.each(data, function(i, item) {
						$('#bookReview > tbody:last').append(
								'<tr>' + '<td>' + item.username + '</td>'
										+ '<td>' + item.mark + '</td>' + '<td>'
										+ item.content + '</td>' + '</tr>');
					});
				});
			}

			$(document).ready(function() {
				getBookInfo(<%=request.getParameter("id")%>);
		getBookReview(<%=request.getParameter("id")%>
			);
			});
		</script>
	</stripes:layout-component>

	<stripes:layout-component name="contents">
		<%
			if (SessionAttributes.isAdmin(request)) {
		%>
		<form action="updatestock" method="post">
			<input type="text" name="quantity"> <input type="hidden"
				name="bookId" value="<%=request.getParameter("id")%>"> <input
				type="submit" value="Add to stock">
		</form>

		<form action="deletebook" method="post">
			<input type="hidden" name="bookId"
				value="<%=request.getParameter("id")%>"> <input
				type="submit" value="Delete book">
		</form>

		<form action="modifybook.jsp" method="post">
			<input type="hidden" name="id"
				value="<%=request.getParameter("id")%>"> <input
				type="submit" value="Modify book">
		</form>
		<%
			}
		%>
		<table id="bookInfo">
			<tbody>
				<tr>
					<th>Title</th>
					<th>Author</th>
					<th>Price</th>
					<th>Description</th>
					<%
						if (SessionAttributes.isAdmin(request)) {
					%>
					<th>Quantity</th>
					<%
						}
					%>
				</tr>
			</tbody>
		</table>

		<table id="bookReview">
			<tbody>
				<tr>
					<th>User</th>
					<th>Mark</th>
					<th>Content</th>
				</tr>
			</tbody>
		</table>
		<%
			if (SessionAttributes.isLoggedIn(request)) {
		%>
		<form action="addreview" method="post">
			Review: <input type="text" name="content"><br> Mark: <select
				name="mark">
				<option value="1">1</option>
				<option value="2">2</option>
				<option value="3">3</option>
				<option value="4">4</option>
				<option value="5">5</option>
			</select> <input type="hidden" name="bookId"
				value="<%=request.getParameter("id")%>"> <input type=submit
				value="Add Review">
		</form>
		<%
			}
		%>
	</stripes:layout-component>

</stripes:layout-render>
